/*
 * Copyright 2007 Jeffrey Palm
 */

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;
import javax.swing.*;

public final class NYTimes extends AbstractNewsReader {

  // --------------------------------------------------
  // Implementation of Finder
  // --------------------------------------------------

  public String name() {return "NYTimes";}

  // --------------------------------------------------
  // Implementation of AbstractNewsReader
  // --------------------------------------------------

  Collection findCategories() throws IOException {
    BufferedReader in = reader("http://nytimes.com/");
    String line;
    final Vector links = new Vector();
    while ((line = in.readLine()) != null) {
      line = line.trim();
      // <li id="navJobs"><a href="http://jobmarket.nytimes.com/pages/jobs/">Job Market</a></li>
      if (!line.startsWith("<li id=\"nav")) continue;
      Link l = getLink(line);
      if (l != null) links.add(l);
    }
    return links;
  }

  String title() {
    return "The New York Times";
  }

  String imageURL() {
    return "http://graphics8.nytimes.com/images/misc/nytlogo379x64.gif";
  }

  Collection findArticleLinks(Link l) throws IOException {

    // remove ? from the end to avoid redirects
    BufferedReader in = reader(prettyLink(l));
        
    // Now read the entry (maybe)
    String line;
    List res = new ArrayList();
    while ((line = in.readLine()) != null) {
      line = line.trim();
      //<a href="http://www.nytimes.com/2007/03/11/magazine/11dark.t.html?ref=science" >Out There</a> 
      if (!line.startsWith("<a href=\"http")) continue;
      Link sl = getLink(line);
      if (sl.link.indexOf(l.name.toLowerCase()) == -1) continue;
      res.add(sl);
    }
    return res;
  }

  Article createArticle(Link parent, Link l) throws Exception {
    Article res = new Article(l);
    Map props = new HashMap();
    props.put("Referer",parent.link);
    BufferedReader in = reader(prettyLink(l));
    String line;
    boolean inText = false;
    while ((line = in.readLine()) != null) {
      line = line.trim();

      if (inText) {
        if (line.startsWith("<p>")) {

          // Make these into paragraphs
          List lines = Util.split(line,"</p>");
          if (lines != null) {
            for (Iterator it = lines.iterator(); it.hasNext();) {
              res.addContent(Util.removeTags((String)it.next()));
              res.addContent("\n\n");
           } 
          }
        }
      }

      //<NYT_TEXT
      if (line.indexOf("<NYT_TEXT") != -1) {
        inText = true;
      }

      //</NYT_TEXT>
      if (line.indexOf("</NYT_TEXT") != -1) {
        inText = false;
        break;
      }

      //<div class="timestamp">Published: March 11, 2007</div>
      if (line.startsWith("<div class=\"timestamp\">")) {
        String date = parseDate(line);
        if (date != null) res.setDate(date);
      }
      
      // <meta  name="byl" content="By HENRY FOUNTAIN">
      if (line.indexOf("name=\"byl\"") != -1) {
        String author = parseAuthor(line);
        if (author != null) res.setAuthor(author);
      }
      
    }
    return res;
  }

  // --------------------------------------------------
  // ReallyAbstractFinder
  // --------------------------------------------------

  String[] getCookies() {
    // these are mine
    return new String[] {
      "TData", "",
      "ANXD", "x",
      "TID", "00g3iga12v8jc6",
      "NYT-S", "0MMaQ/2qmmFHvDXrmvxADeHALZmbKL9T0VdeFz9JchiAKeH8OHt5uoRYV.Ynx4rkFI",
      "WT_FPC", "id=70.180.226.109-1966019120.29844530:lv=1173661762879:ss=1173661762879",
      "zFD", "73B10730110700101",
      "zFN", "73B10730110700101",
      "ups", "73BNAR1eOkWA0L3T",
      "up",  "73BNAR1eOkWA0L3T",
      "s_sess", "%20s_cc%3Dtrue%3B%20s_sq%3D%3B",
      "s_pers", "%20s_vsn_nytimesglobal_1%3D5640290924485%7C1489277359523%3B%20s_vnum1%3D1173654559589%257C1%7C1489277359589%3B",
      "adxcl", "tcasale06-mar07-rem-505470=4636bb3f:1173654620|tadcom05-mar07-rem-506997=4636bb3f:1173654620",
      "NYT_GR", "45f4910c-4dqh3/JLbLUMj75I/6LH8g",
      "RMID", "3324742636a445f48c5cd0dc",
      "ACOOKIE", "C8ctADcwLjE4MC4yMjYuMTA5LTE5NjYwMTkxMjAuMjk4NDQ1MzAAAAAAAAABAAAA2asAAGeM9EVnjPRFAQAAAC1EAABnjPRFZ4z0RQAAAAA-",
      "s_vi", "[CS]v1|45F48C6300006F54-A290A9E00005E1E[CE]",
    };
  }

  // --------------------------------------------------
  // Misc
  // --------------------------------------------------

  private Link getLink(String line) {
    String href = "href=\"";
    int ihref = line.indexOf(href);
    if (ihref == -1) return null;
    int iquote = line.indexOf("\"",ihref+href.length());
    if (iquote == -1) return null;
    String link = line.substring(ihref+href.length(),iquote).trim();
    String name = innerHTML(line,iquote);
    Link l = new Link(name,link);
    note("Found: " + l.name + " -- " + l.link);
    return l;
  }

  private String parseDate(String line) {
    //<div class="timestamp">Published: March 11, 2007</div>
    int icolon = line.indexOf(":");
    if (icolon == -1) return null;
    int ilt = line.indexOf("<",icolon);
    if (ilt == -1) return null;
    return line.substring(icolon+1,ilt);
  }

  private String parseAuthor(String line) {
    //<meta  name="byl" content="By HENRY FOUNTAIN">
    String target = "content=\"By ";
    int itarget = line.indexOf(target);
    if (itarget == -1) return null;
    int iq = line.indexOf("\"",itarget+target.length());
    if (iq == -1) return null;
    return line.substring(itarget+target.length(),iq);
  }

  // --------------------------------------------------
  // Testing
  // --------------------------------------------------

  public static void main(String[] args) throws Exception {
    new NYTimes().find(new IPod(),Progress.DEFAULT);
  }
}